/**
 * Created by Aimee on 14-11-12.
 */
define(['jquery', 'jquerymobile', 'net', 'dialogs'], function($, m, net, dia) {

    var confirm_activity = null;
    var confirm_needStr = null;

    // 补充0
    function pad(n, width, z) {
        z = z || '0';
        n = n + '';
        return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
    }

    function getMonthString(month) {
        month = month + 1;
        if (month == 1)
            return 'Jan';
        else if (month == 2)
            return 'Feb';
        else if (month == 3)
            return 'Mar';
        else if (month == 4)
            return 'Apr';
        else if (month == 5)
            return 'May';
        else if (month == 6)
            return 'June';
        else if (month == 7)
            return 'July';
        else if (month == 8)
            return 'Aug';
        else if (month == 9)
            return 'Sept';
        else if (month == 10)
            return 'Oct';
        else if (month == 11)
            return 'Nov';
        else if (month == 12)
            return 'Dec';
        else {
            console.assert(false, '月份 %o 不存在 ', month );
        }
    }

    function get_verifyAcvtivity(activity) {
        var value = activity;

        //解析报名信息
        var need_info = confirm_needStr;
//        need_info = value.enroll_need;
        console.assert(need_info != null, '必须有报名文本框显示 %o', need_info);
        var needArr = need_info.split('==');

        console.log('各文本框的属性：%o', needArr);

        //获取活动
        var startDate = new Date(value.startTime * 1000);
        var endDate = new Date(value.endTime * 1000);
        var startTime = 'Start: ' + pad(startDate.getHours(), 2) + ':' + pad(startDate.getMinutes(),2) + ', ' + startDate.getDate() + " " + getMonthString(startDate.getMonth()) + " " + startDate.getFullYear();
        var endTime = 'End: ' + pad(endDate.getHours(),2) + ':' + pad(endDate.getMinutes(),2) + ', ' + endDate.getDate() + " " + getMonthString(endDate.getMonth()) + " " + endDate.getFullYear();
        var venue = 'Venue: ' + value.venue;
        var li = creatActivity(value.subject, startTime, endTime, venue, needArr);
        $(li).insertBefore($('#activityConfirm_list li:first'));
    }

    //创建活动
    function creatActivity(subject, startTime, endTime, venue, needArr) {
        var html_text = '';
        if(needArr != null){
//            var i = 0;
            $.each(needArr, function(index, val) {
                var nameArr = val.split(":");
                console.log('nameAttr: %o', nameArr);
                if(nameArr[1] == 'yes') {
                    html_text += html_text = '\<!--'+ nameArr[0] +'\-->\
                        <div style="width:100%;padding: 10px 10px 0px 10px">\
                        <div style="font-family: Arial;font-size: 14px;font-weight: normal;color: #404040">'+ nameArr[0] +'\</div>\
                        <div id="activityVerify_id" style="width: 100%;height: auto;font-family: Arial;font-size: 16px;font-weight: bold;color: #404040;">'+ nameArr[2] +'\</div>\
                    </div>';
//                    i++;
                } else if(nameArr[1] == 'no'){
                    html_text += html_text = '\<!--'+ nameArr[0] +'\-->\
                        <div style="width:100%;padding: 10px 10px 0px 10px">\
                       <div style="font-family: Arial;font-size: 14px;font-weight: normal;color: #404040">'+ nameArr[0] +'\</div>\
                       <div id="activityVerify_id" style="width: 100%;height: auto;font-family: Arial;font-size: 16px;font-weight: bold;color: #404040;">'+ nameArr[2] +'\&nbsp;</div></div>';
//                    i++;
                } else {
                    console.log(false, '文本框只有必填和非必填两种可能');
                }
            });
        } else {
            console.assert(needArr != null, '文本框为空 %o', needArr);
        }

        var html = '\<li style="padding: 0px;">\
            <!--标题图-->\
            <div style="width:100%;  padding: 0; border: none;background-color: rgb(101,153,193);">\
                <div style="width:100%;padding: 20px 10px 10px 10px">\
                    <div style="white-space:normal;font-weight: normal;font-size: 18px;color: #ffffff;margin-bottom: 8px">'+ subject +'\</div>\
                    <div style="font-size: 11px;color: #ffffff;line-height: 18px;font-weight: normal">\
                        <span style="display: block">'+ startTime +'\</span>\
                        <span style="display: block">'+ endTime +'\</span>\
                        <span style="display: block">'+ venue +'\</span>\
                    </div>\
                </div>\
            </div>\
            <div style="border: solid 1px rgb(223,223,223);padding: 10px 10px 10px 3px;overflow: auto;margin: 10px">\
            <div class="confirm_ok" style="float: left;display: inline;height: 38px;width: 38px"></div>\
                <div style="display: inline;white-space: normal">\
                Thank you very much for your registration!\
                </div>\
            </div>\
            <div style="padding-bottom: 10px;">\
             '+ html_text +'\
            \</div>\
            \</li>';
        return html;
    }

    //返回到活动注册页面
    $('#activityConfirm_btn_ok').off('click')
        .on('click', function() {
			if(window.pageFrom){
				$.mobile.newChangePage(window.pageFrom,{ transition: "slide",reverse: true, changeHash:false});
			}else{
				$.mobile.newChangePage('#activities',{ transition: "slide",reverse: true, changeHash:false});
			}
        });

    //添加到日历
    $('#btn_activityAddCalendar').off('click')
        .on('click', function() {
            dia.alert('Oops!',"add to calendar！",['OK'],function(title) {

            });
        });

    $('#activityConfirm').on('pageshow', function(event) {
        window.setBodyOverflow($(document.body));
        $('#activityConfirm_content').css('height',($(window).height()-40));

        get_verifyAcvtivity(confirm_activity);
    });

    $("#activityConfirm").on( "pagehide", function( event ) {
        $('#activityConfirm_list li:first').empty();
    });

    return {
        showConfirmActivity: function(activity){
            confirm_activity = activity;
        },
        showConfirmNeedStr: function(needStr) {
            confirm_needStr = needStr;
        }
    }

});